package com.admin.system.controller;

import com.admin.base.BaseController;
import com.admin.base.Response;
import com.admin.base.ResponseType;
import com.admin.system.auth.entity.LoginToken;
import com.admin.system.entity.SystemIcon;
import com.admin.system.entity.SystemResource;
import com.admin.system.entity.SystemUser;
import com.admin.system.service.SystemIconService;
import com.admin.system.service.SystemResourceService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class HomeController extends BaseController {
    @Autowired
    SystemResourceService resourceService;
    @Autowired
    SystemIconService iconService;

    @RequestMapping(value = "/loginPage")
    public ModelAndView loginPage(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        Subject subject = SecurityUtils.getSubject();
        SystemUser loginUser = (SystemUser) subject.getPrincipal();
        if (loginUser != null) {
            //用户已登录，转发首页
            mav.setViewName("redirect:index");
        } else {
            mav.setViewName("login");
        }

        return mav;
    }

    /**
     * 登录
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public Response login(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        Subject subject = SecurityUtils.getSubject();
        LoginToken token = new LoginToken();
        token.setUserName(userName);
        token.setPassword(password);
        // 进行验证，这里可以捕获异常，然后返回对应信息
        try {
            subject.login(token);
        } catch (DisabledAccountException de) {
            return Response.ok(ResponseType.ACCOUNT_DISABLED);
        } catch (AccountException e) {
            return Response.ok(ResponseType.ACCOUNT_ERROR);
        } catch (CredentialsException ce) {
            return Response.ok(ResponseType.ACCOUNT_ERROR);
        }
        return Response.ok();
    }

    @RequestMapping(value = "/logout")
    public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        // TODO 目前使用shiro自身登出功能
        return mav;
    }

    /**
     * 首页
     *
     * @param request
     * @param response
     * @param mav
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        SystemUser user = getLoginUser();
        List<SystemResource> resources = resourceService.querySystemResources(user.getId());
        mav.addObject("resources", resources);
        mav.addObject("user", user);
        mav.setViewName("index");
        return mav;
    }

    @RequestMapping(value = "/portal")
    public ModelAndView welcome(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        SystemUser user = getLoginUser();
        mav.addObject("user", user);
        mav.setViewName("portal");
        return mav;
    }

    /**
     * unicode图标列表
     *
     * @param request
     * @param response
     * @param mav
     * @return
     */
    @RequestMapping(value = "/iconFont")
    public ModelAndView iconFont(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {

        List<SystemIcon> icons = iconService.getAll();
        mav.addObject("icons", icons);
        mav.setViewName("system/systemIcon/unicode");
        return mav;
    }

    /**
     * unicode图标列表及说明
     *
     * @param request
     * @param response
     * @param mav
     * @return
     */
    @RequestMapping(value = "/unicode")
    public ModelAndView unicode(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        List<SystemIcon> icons = iconService.getAll();
        mav.addObject("icons", icons);
        mav.setViewName("unicode");
        return mav;
    }

//    @RequestMapping(name = "/gateWay",consumes="application/json",produces ="application/json;charset=utf-8",method = RequestMethod.POST)
//    @RequestMapping(name = "/gateWay")
//    public ModelAndView gateWay(HttpServletRequest request) {
//        String data = request.getParameter("data");
//        BaseResponse response = new BaseResponse();
//        response.setReturnCode("0");
//        response.setReturnMsg("success");
//        return null;
//    }

//    @RequestMapping(value = "/gateWay")
//    @ResponseBody
//    public BaseResponse gateWay(HttpServletRequest request) {
//        BaseResponse response = new BaseResponse();
//        response.setReturnCode("0");
//        response.setReturnMsg("success");
//        return response;
//    }
}